<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2018/3/12
 * Time: 16:08
 */
namespace app\home\controller;


use app\BaseController;
use app\common\model\mysql\Category;
use think\App;

use think\exception\HttpResponseException;
use think\facade\View;


class Base extends BaseController
{
    public $userId = null;
    public $noVery = ['Login\login','Login\dologin','Login\register','Login\phone','Login\sendcode','Login\logout','Index\index','Goods\index','Goods\detail','Goods\detail'];

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub


        $route = $this->request->controller().'\\'.$this->request->action();

        if(!in_array($route,$this->noVery)){

            if(empty(session('user_info'))){
                $this->redirect('/login/login');
            }
            $this->userId = session('user_info')['id'];
        }

        //查询分类细腻
        $category = Category::select()->toArray();
        //转化为父子级的树状结构
        $category = get_tree_list($category);

        View::assign('category',$category);
    }

    public function response($code=200, $msg='success', $data=[])
    {
        $res = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
//        echo json_encode($res, JSON_UNESCAPED_UNICODE);die;
        json($res)->send();die;
    }

    public function fail($msg='fail',$code=500)
    {
         $this->response($code, $msg);
    }
    public function ok($data=[], $code=200, $msg='success')
    {
         $this->response($code, $msg, $data);
    }


    public function redirected(...$args){
        throw new HttpResponseException(redirect(...$args));
    }




}